package xhttp

import (
	"boy-go/pkg/constants"
	"fmt"
	"golang.org/x/text/encoding/simplifiedchinese"
	"golang.org/x/text/transform"
	"io"
	"net/http"
)

func SendGet(url string, param *string, contentType *string) string {
	urlPath := url
	if param != nil {
		urlPath += "?" + *param
	}
	// 发送 GET 请求
	resp, err := http.Get(urlPath)
	if err != nil {
		fmt.Println("Error sending GET request:", err)
		return ""
	}
	defer resp.Body.Close() // 确保关闭响应体
	result := ""
	//转码
	if contentType != nil && *contentType == constants.GBK {
		decoder := simplifiedchinese.GBK.NewDecoder()
		reader := transform.NewReader(resp.Body, decoder)
		// 读取并解码内容
		content, err := io.ReadAll(reader)
		if err != nil {
			return ""
		}
		result = string(content)
	} else {
		// 读取响应体
		body, err := io.ReadAll(resp.Body)
		if err != nil {
			fmt.Println("Error reading response body:", err)
			return ""
		}
		result = string(body)
	}
	fmt.Println("Response Body:", result)
	return result
}

func SendPost(urlPath string) {

}

func SendPut(urlPath string) {

}
